package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostNoticeTypeDao;
import ysq.xyg.host.model.dto.HostNoticeTypeDto;
import ysq.xyg.host.model.po.HostNoticeType;
import ysq.xyg.host.model.vo.HostNoticeTypeVo;
import ysq.xyg.host.service.HostNoticeTypeService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房产-通知公告类型service实现
 *
 * @author congcong tao
 * @date 2019-12-13 11:08:30
 */
@Service("hostNoticeTypeService")
public class HostNoticeTypeServiceImpl extends BaseServiceImpl<HostNoticeType> implements HostNoticeTypeService {

    @Autowired
    private HostNoticeTypeDao hostNoticeTypeDao;

    @Autowired
    public void setBaseDao(HostNoticeTypeDao hostNoticeTypeDao) {
        this.baseDao = hostNoticeTypeDao;
    }

    /**
     * 分页查询
     *
     * @param hostNoticeTypeVo 对象
     * @param pageSize         展示数量
     * @param pageNum          当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 11:08:30
     */
    @Override
    public ReturnEntity page(HostNoticeTypeVo hostNoticeTypeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" order_num asc, update_time desc");
        List<HostNoticeType> list = acceptList(hostNoticeTypeVo);
        PageInfo<HostNoticeType> page = new PageInfo<HostNoticeType>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostNoticeTypeVo 对象
     * @param currentUser      当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 11:08:30
     */
    @Override
    @CacheEvict(value={"cacheHostNoticeTypeByAccountIdList"}, allEntries=true)
    public ReturnEntity addOrUpdate(HostNoticeTypeVo hostNoticeTypeVo, SysUser currentUser) {
        boolean isUpate = true;
        HostNoticeType hostNoticeType;
        if (StrUtil.isEmpty(hostNoticeTypeVo.getId())) {
            hostNoticeType = new HostNoticeType(currentUser);
            isUpate = false;
        } else {
            hostNoticeType = selectByPrimaryKey(hostNoticeTypeVo.getId());
            if (ObjectUtil.isNull(hostNoticeType)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostNoticeType.setUpdateTime(new Date());
            hostNoticeType.setUpdateUser(currentUser.getId());
        }
        hostNoticeType.setName(hostNoticeTypeVo.getName());
        hostNoticeType.setOrderNum(hostNoticeTypeVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostNoticeType);
        } else {
            num = insert(hostNoticeType);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id          主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-13 11:08:30
     */
    @Override
    @CacheEvict(value={"cacheHostNoticeTypeByAccountIdList"}, allEntries=true)
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostNoticeType hostNoticeType = selectByPrimaryKey(id);
        if (hostNoticeType != null) {
            hostNoticeType.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostNoticeType.setUpdateTime(new Date());
            hostNoticeType.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostNoticeType);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostNoticeTypeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-13 11:08:30
     */
    @Override
    public List<HostNoticeType> acceptList(HostNoticeTypeVo hostNoticeTypeVo) {
        Example example = new Example(HostNoticeType.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostNoticeTypeVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostNoticeTypeVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostNoticeTypeVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostNoticeTypeVo.getAccountId());
        }
        example.setOrderByClause(" order_num asc, update_time desc");
        return hostNoticeTypeDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code, String accountId) {
        HostNoticeTypeVo hostNoticeTypeVo = new HostNoticeTypeVo();
        hostNoticeTypeVo.setAccountId(accountId);
        List<HostNoticeType> list = acceptList(hostNoticeTypeVo);
        List<SysDictContent> sysDictContentList = new ArrayList<SysDictContent>();
        for (HostNoticeType entity : list) {
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }

    @Override
    public List<HostNoticeTypeDto> selectHostNoticeTypeList(String accountId) {
        return hostNoticeTypeDao.selectHostNoticeTypeList(accountId);
    }
}
